package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;



    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        if (page==1){
            page=0;
        }
       List<Supplier> supplierList = supplierDao.pageList(page,rows,supplierName);

        Map<String, Object> map = new HashMap<>();
        map.put("total", supplierList.size());

//        List<Supplier> list = new ArrayList<>();
//
//        supplierList.stream().forEach(supplier -> {
//                    Map supperMap = new HashMap<>();
//                    supperMap.put("supplierId", supplier.getSupplierId());
//                    supperMap.put("supplierName", supplier.getSupplierName());
//                    supperMap.put("contacts", supplier.getContacts());
//                    supperMap.put("phoneNumber", supplier.getPhoneNumber());
//                    supperMap.put("address", supplier.getAddress());
//                    supperMap.put("remarks", supplier.getRemarks());
//                    list.add(supplier);
//        });
        map.put("rows",supplierList);
        return map;
    }

    //供应商添加或修改
    @Override
    public void saveAndUpdate(Integer supplierId, Supplier supplier) {

        if (supplierId != null){
            supplierDao.deleteSupplier(supplierId);
        }
       supplierDao.saveSupplier(supplier);
    }


    //删除供应商（支持批量删除）
    @Override
    public void delete(String ids) {
        String[] supplierId = ids.split(",");
        List<String> arrayList = Arrays.asList(supplierId);
        supplierDao.deleteSuppliers(arrayList);
    }


}
